package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTradeMarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    //调用mapper层
    @Autowired
    private BaseTradeMarkMapper baseTradeMarkMapper;

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    /**
     * 根据category3Id获取品牌列表
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //select * from base_category_trademark where category3_id = category3Id;
        //先根据三级分类id获取品牌id
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        //需要trademark_id
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);
        //判断
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
//            List<Long> idList = new ArrayList<>();
//            //获取品牌id集合
//            for (BaseCategoryTrademark baseCategoryTrademark : baseCategoryTrademarkList) {
//                Long trademarkId = baseCategoryTrademark.getTrademarkId();
//                idList.add(trademarkId);
//            }
            //只有函数式接口才能使用拉姆达表达式  复制小括号，写死右箭头，落地大括号
            List<Long> idList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            //获取品牌id集合
            return baseTradeMarkMapper.selectBatchIds(idList);
        }
        return null;
    }

    /**
     * 删除分类品牌关联
     * @param category3Id
     * @param trademarkId
     */
    @Override
    public void remove(Long category3Id, Long trademarkId) {
        //delete from base_trademark where trademark_id = trademarkId and category3_id = category3Id;
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        baseCategoryTrademarkQueryWrapper.eq("trademark_id", trademarkId);
        //删除数据
        baseCategoryTrademarkMapper.delete(baseCategoryTrademarkQueryWrapper);
    }

    /**
     * 根据分类id来获取可选中的品牌数
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //业务逻辑，获取可选品牌，不在这个分类数据对应的品牌Id中
        //手机： 1,3  所有品牌Id 有1,2,3  点击添加品牌的时候，只能显示2；
        //先根据三级分类id获取品牌id
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);
        //判断
        if(!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //1,3
            List<Long> idList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            //1,2,3 只留2
            return baseTradeMarkMapper.selectList(null).stream().filter(baseTrademark -> {
                return !idList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
        }
        //当这个分类id下没有品牌的时候  显示所有品牌数据
        return baseTradeMarkMapper.selectList(null);
    }

    /**
     * 保存分类对应的品牌数据
     * @param categoryTrademarkVo
     */
    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //本质插入数据base_category_trademark
        //获取对应的tmId 1,3
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        //遍历
        if(!CollectionUtils.isEmpty(trademarkIdList)){
            List<BaseCategoryTrademark> baseCategoryTrademarkList = new ArrayList<>();
            //插入
            for (Long tmId : trademarkIdList) {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(tmId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                baseCategoryTrademarkList.add(baseCategoryTrademark);
//                baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
            }
            //保存
            this.saveBatch(baseCategoryTrademarkList);
            //拉姆达表达式写法
//            List<BaseCategoryTrademark> collect = trademarkIdList.stream().map((tmId) -> {
//                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
//                baseCategoryTrademark.setTrademarkId(tmId);
//                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
//                return baseCategoryTrademark;
//            }).collect(Collectors.toList());
//            //保存
//            this.saveBatch(collect);
        }

    }
}
